// pages/collection/collection.js
const app = getApp();
var router = require("../../router.js");
var audioAction = {};
audioAction.method = 'pause';
var audioPay = {};
audioPay.method = 'play';
//音频播放
const backgroundAudioManager = wx.getBackgroundAudioManager();
const recorderManager = wx.getRecorderManager();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [    
    ],
    page: 0,
    info: false,
    payIndex: "",
    scrollTop: 0,   
  },

  // audio: function (event) {
  //   var index = event.target.dataset.ctr;
  //   var payIndex = this.data.payIndex;
  //   var list = this.data.list;
  //   if (payIndex !== "" && payIndex != index) {
  //     list[payIndex].audioAction = audioAction;
  //     list[index].audioAction = audioPay;
  //     this.setData({
  //       list: list,
  //       payIndex: index
  //     })
  //   }
  //   else if (index === payIndex) {
  //     list[payIndex].audioAction = audioAction;
  //     this.setData({
  //       list: list,
  //       payIndex: ""
  //     });
  //   } else {
  //     list[index].audioAction = audioPay;
  //     this.setData({
  //       list: list,
  //       payIndex: index
  //     });
  //   }
  // },
  //音频播放
  start_chosedeil: function (event) {
    var that = this;
    var index = event.target.dataset.ctr;  //获取每个的id
    var list = that.data.list;
    for (var i = 0; i < list.length; i++) {
      if (i != index) {
        list[i].picB = true
      }
    }

    if (list[index].picB) {
      list[index].picB = false
    } else {
      list[index].picB = true
    }
    if (list[index].picB == false) {
      wx.playBackgroundAudio({
        dataUrl: list[index].music,
        //图片地址地址
      })
    } else {
      backgroundAudioManager.pause();
    }
    this.setData({
      // picB: ic,
      index: index,
      list: list
    })
  },

  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      return {
        title: that.data.list[index].title,
        path: '/pages/collection/collection',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
  //点赞
  toCollectother: function (event) {
    var that = this;
    var index = event.target.dataset.ctr;
    var id = event.target.dataset.id;
    app.request({
      url: router.msg.praise,
      method: "POST",
      data: {
        uid: wx.getStorageSync('uid'),
        id: id,
      },
      success: function (data) {
        if (data) {
          var list = that.data.list;
          var status = list[index].praise_type;
          list[index].praise_type = !status;
          var praise = list[index].praise;
          if (status) {
            list[index].praise = praise + 1;
          } else {
            var newPraise = praise - 1;
            if (newPraise < 0) {
              newPraise = 0;
            }
            list[index].praise = newPraise;
          }
          that.setData({
            list: list
          })
          // 交互反馈
          wx.showToast({
            title: list[index].praise_type ? '取消' : '点赞',
            icon: 'success',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: '操作失败请重试',
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  //收藏
  toCollection: function (event) {
    var that = this;
    var index = event.target.dataset.ctr;
    var id = event.target.dataset.id;
    app.request({
      url: router.msg.collect,
      method: "POST",
      data: {
        uid: wx.getStorageSync('uid'),
        id: id,
      },
      success: function (data) {
        if (data) {
          var list = that.data.list;
          var status = list[index].collect_type;
          list[index].collect_type = !status;
          var collect = list[index].collect;
          if (status) {
            list[index].collect = collect + 1;
          } else {                  
            list[index].showInfo = false;
            list[index].collect = collect - 1;
          }         
          that.setData({
            list: list
          })
          // 交互反馈
          wx.showToast({
            title: list[index].collect_type ? '取消' : '收藏',
            icon: 'success',
            duration: 2000
          });
        } else {
          wx.showToast({
            title: '操作失败请重试',
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },

  lower: function (e) {
    this.getInfo();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    app.isLogin();
    wx.showShareMenu({
      withShareTicket: true
    })
    this.setData({
      height: wx.getSystemInfoSync().windowHeight
    });  
  },

  onShow: function () {
    let that = this;
    this.setData({
      scrollTop: 0,
      page: 0,
      list: []
    });
    this.getInfo();
    wx.showShareMenu({
      withShareTicket: true
    })
    //监听音乐时长
    backgroundAudioManager.onTimeUpdate(function () {
      var list = that.data.list;
      var index = that.data.index;    //索引
      var currentTime = backgroundAudioManager.currentTime  //当前时长
      // var duration_t = Math.floor(duration / 60) + ":" + (duration % 60 / 100).toFixed(2).slice(-2);
      var currentTime_t = Math.floor(currentTime / 60) + ":" + (currentTime % 60 / 100).toFixed(2).slice(-2);
      for (var i = 0; i < list.length; i++) {
        list[index].currentTime = currentTime_t
      }
      that.setData({
        list: list
      })      
    })
    //监听音乐自然结束
    backgroundAudioManager.onEnded(function () {
      var list = that.data.list;
      for (var i = 0; i < list.length; i++) {
        list[i].picB = true
      }
      that.setData({
        list: list
      })
    })
  },
  getInfo: function () {
    var that = this;
    app.request({
      url: router.msg.collectList,
      method: "POST",
      data: {
        uid: wx.getStorageSync('uid'),
        page: that.data.page,       
      },
      success: function (data) {      
        if (data.length > 0) {
          for (var i = 0; i < data.length; i++) {
              data[i].audioAction = audioAction,
              data[i].currentTime = '0:00',
              data[i].picB = true
              data[i].showInfo = true,
              that.data.list.push(data[i]);
          }
          that.setData({
            list: that.data.list,
            page: that.data.page + 1
          });
        } else {
          that.setData({
            info: true,
          });
        }
      }
    })
  }
})